package com.wjerp.tplus.server.common.utils;


import javax.servlet.http.HttpServletRequest;

/**
 * 获取客户端IP地址
 * Created with IntelliJ IDEA.
 * User: mac
 * Date: 17/4/1
 * Time: 下午4:05
 */
public class IpUtil {

  /**
   * 获取请求的Ip地址
   *
   * @param request
   * @return
   */
  public static String getIpAddress(HttpServletRequest request) {
    String ip = request.getHeader("X-Real-IP");
//        String ip = request.getHeader("x-forwarded-for");
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("X-Forwarded-For");
    } else if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("Proxy-Client-IP");
    } else if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("WL-Proxy-Client-IP");
    }

    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getRemoteAddr();
    }
    return ip;
  }
}
